package TeachLesson;

import TeachLesson.Res.ResDao;
import TeachLesson.Res.ResEnity;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;

@WebServlet(name = "UploadKonwledgeFileServlet")
public class UploadKonwledgeFileServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        response.setCharacterEncoding("GBK");
        request.setCharacterEncoding("GBK");
        //创建su
        SmartUpload su=new SmartUpload();
        su.initialize(getServletConfig(),request,response);
        su.setMaxFileSize(1024*1024*10);
        try {
            su.upload();
            //获取参数
            String point=su.getRequest().getParameter("point");
            String dirName=su.getRequest().getParameter("dirName");
            String courseNo=su.getRequest().getParameter("courseNo");
            String workerNo=su.getRequest().getParameter("workerNo");


            //获取文件路径
            String filePath="F:\\JWGLXT\\web\\StoreResoures\\"+workerNo+"\\"+courseNo+"\\"+dirName+"\\"+point;

            //获取上传的文件
            com.jspsmart.upload.File tempFile = su.getFiles().getFile(0);
            String fileName=tempFile.getFileName();

            //插入数据库
            ResDao resDao=new ResDao();
            resDao.addRes(workerNo,courseNo,filePath,fileName);

            response.getWriter().println(filePath);
            su.save(filePath);
            response.sendRedirect("/Teachers/knowledgeResourses.jsp?point="+ URLEncoder.encode(point,"UTF-8")+"&dirName="+URLEncoder.encode(dirName,"UTF-8"));
        } catch (SmartUploadException e) {
            e.printStackTrace();
        }
    }
}
